package com.superhelper.app.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import com.superhelper.app.domain.DeliverRecordOrder;
import com.superhelper.app.domain.PageModel;
import com.superhelper.common.domain.AuserDeliver;
import com.superhelper.common.domain.DeliverAccount;
import com.superhelper.common.domain.DeliverRecord;
import com.superhelper.common.domain.WifiAccount;

/**
 * 
 * @author Administrator
 *
 */

@Component("com.superhelper.superhelper_web.mapper.DeliverMapper")
public interface DeliverMapper {

	@Insert("replace into auser_deliver_conf(auserId,normal_sec,interval_sec,gt_money,delay_sec,"
			+ "fixed_minute,part_money,gt_minute,max_money,state,one_km_minute,"
			+ "two_km_minute,third_km_minute,other_km_minute)values"
			+ "(#{a.auserId},#{a.normal_sec},#{a.interval_sec},#{a.gt_money},"
			+ "#{a.delay_sec},#{a.fixed_minute},#{a.part_money},#{a.gt_minute},"
			+ "#{a.max_money},#{a.state},#{a.one_km_minute},"
			+ "#{a.two_km_minute},#{a.third_km_minute},#{a.other_km_minute})")
	int addAuserDeviler(@Param("a") AuserDeliver entity);

	@Select("select * from auser_deliver_conf where auserId=#{auserId} ")
	AuserDeliver getAuserDeliver(@Param("auserId") Long auserId);

	/**
	 * 已发单数据
	 * 
	 * @param orderIds
	 * @return
	 */
	@Select("select orderId from deliver_record where orderId in(${orderIds}) and status not in(5)")
	List<Long> getHasDeliverRecordByOrderIds(@Param("orderIds") String orderIds);

	@Select("select * from  deliver_record  where id=#{id}")
	DeliverRecordOrder getDeliverRecordOrder(@Param("id") Long id);

	@Select("select * from  deliver_record  where orderId=#{orderId} and status not in(4) order by createAt desc limit 1")
	DeliverRecordOrder getNeedCancelDeliverRecordByOrderId(
			@Param("orderId") Long orderId);

	@Select("select count(*) from deliver_record where auserShopId in(${shopIds}) and status=#{status} and  createAt>date_sub(curdate(),interval 0 day)")
	int getDeliverRecordListCount(@Param("shopIds") String shopIds,
			@Param("status") Integer status, @Param("timeAt") Date timeAt);

	@Select("select * from deliver_record where auserShopId in(${shopIds}) and status=#{status} and createAt<#{timeAt} and createAt>date_sub(curdate(),interval 0 day) "
			+ "order by createAt desc limit #{pageSize}")
	List<DeliverRecord> getDeliverRecordList(@Param("shopIds") String shopIds,
			@Param("status") Integer status,
			@Param("pageSize") Integer pageSize, @Param("timeAt") Date timeAt);

	@Update("update deliver_record set currentTip=currentTip+#{tips} where id=#{id}")
	int addTips(@Param("id") Long id, @Param("tips") Double tips);

	@Update("update deliver_record set status=5,statusMsg='商户取消订单',statusChangeAt=now() where id=#{id}")
	int cancelRecord(@Param("id") Long id);

	@Update("update auser_deliver_conf set state=#{state} where auserId=#{auserId}")
	int updateDeliverSwitch(@Param("auserId") Long auserId,
			@Param("state") Integer state);

	@Select("select * from deliver_account where auserId=#{auserId} ")
	List<DeliverAccount> getDeliverAccountList(@Param("auserId") Long auserId);

	@Insert("replace into deliver_account(auserId,deliverUid,deliverShopNo,deliverType)values(#{a.auserId},#{a.deliverUid},#{a.deliverShopNo},#{a.deliverType})")
	int addDeliverAccount(@Param("a") DeliverAccount entity);

	@Insert("replace into wifi_account(auserId,sn,secret,type)values(#{a.auserId},#{a.sn},#{a.secret},#{a.type})")
	int addWifiAccount(@Param("a") WifiAccount entity);

	@Select("select * from deliver_account where auserId=#{auserId} and deliverType=#{deliverType}")
	DeliverAccount getDeliverAccountByType(@Param("auserId") Long auserId,
			@Param("deliverType") Integer deliverType);

	@Delete("delete from task where objId=#{objId} ")
	int deleteTask(@Param("objId") String objId);

	@Delete("delete from task where objId in(select id from deliver_record where status=0 and orderId in"
			+ "(select id from takeout_order where createAt>date_sub(curdate(),interval 0 day) and shopId=#{shopId} and type=#{type})"
			+ ") ")
	int deleteTaskByStopAuto(@Param("type") String type,
			@Param("shopId") String shopId);

	@Select("select count(*) from deliver_record where status=0 and auserShopId=#{auserId} and createAt>date_sub(curdate(),interval 0 day)")
	int getWaitingTask(@Param("auserId") Long auserId);
	
	@Select("select * from wifi_account where auserId=#{auserId}")
	List<WifiAccount> getWifiAccountList(@Param("auserId") Long auserId);
}
